package com.kdtech.analyse.video;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.StringUtils;
import com.kdtech.analyse.AnalyseNews;

/**
 * 重庆电视台
 * @author Administrator
 * abc
 */
public class CbgAnalyse implements AnalyseNews{
	
	public boolean isDetailPage(String url) {
		boolean bRet = false;
		String[] regex = {
				"http://.*.cbg.cn/html/.*[0-9]{4}-[0-9]{2}/[0-9]{2}/content_[0-9]{8}.htm"
				,"http://.*.cbg.cn/.*[0-9]{4}/[0-9]{4}/[0-9].*.shtml"
				};
		for (int i = 0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return bRet;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta video = new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt = urlMeta.getHtml();
		String url = urlMeta.getUrl();
		if(!isDetailPage(url)){
		}
		video.setUrl(url);
		String title = null;
		String desc = null;
		Long date = null;
		String dateStr = null;
		Document doc = Jsoup.parse(htmltxt);
		title = doc.select("div#biaoti.div_w1_l_t_title").text();
        if(StringUtils.isBlank(title)){
        	title = doc.select("div.title h2").text();
        	if(StringUtils.isBlank(title)){
        		title = doc.select("h1.h1").text();
        		if(StringUtils.isBlank(title)){
        			title = doc.select("li.li4").text();
        		}
        	}
        }
			desc =StringUtils.substringBetween(htmltxt, "<strong>简介：</strong><span title=\"","\"");
			dateStr =doc.select("div.div_w1_l_t_time,.post-time").text();
			date = DateUtils.matchDate(dateStr);
			if(date==null){
				date=DateUtils.matchDate(url);
			}
			video.setTitle(StringUtils.trimSpace(title));
			video.setContent(desc);
			video.setDate(date);

			String author=doc.select("div.content div.title div.article_head").text();
			if(author!=null && author.indexOf("来源：")!=-1){
				author=StringUtils.substringBetween(author, "来源：", "我要评论");
			}
			video.setAuthor(author);
			return video;
	}
	

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}
	
}
